﻿using System;
using System.Data;
using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;
using System.Web;
using System.Web.Caching;


using LMD.Constant;
using LMD.Lib.Oracle;

namespace Cached
{
    /// <summary>
    /// Summary description for FeedSubscription
    /// </summary>
    public class FeedSubscription : CacheAbstraction
    {
        public DataTable GetFeedSubscriptionList(int iUserRoleId)
        {
            return GetFeedSubscriptionList(iUserRoleId, false);
        }
        public DataTable GetFeedSubscriptionList(int iUserRoleId, bool bForceRefresh)
        {
            String sxCacheKey = KCache.FEED_SUBSCRIPTION + iUserRoleId;

            if(bForceRefresh)
            {
                HttpRuntime.Cache.Remove(sxCacheKey);
            }

            if (HttpRuntime.Cache[sxCacheKey] == null)
            {
                DBLib dbl = new DBLib();

                OracleParameter param = new OracleParameter();
                param.ParameterName = "prmID";
                param.OracleDbType = OracleDbType.Number;
                param.Value = iUserRoleId;

                String sxQuery =
                    @"
                    select
                        m1.FEED_SOURCE_ID SRC_ID,
                        m1.NAME,
                        t1.T_FEED_SUBSCRIPTION_ID,
                        DECODE(t1.T_FEED_SUBSCRIPTION_ID, null, 'N', 'Y') IS_CHECKED
                    from 
                        FEED_SOURCE m1
                        left join T_FEED_SUBSCRIPTION t1
                            on t1.FEED_SOURCE_ID = m1.FEED_SOURCE_ID
                            and t1.P_ICP_USER_ROLE_ID = :prmID    
                    where         
                        nvl(m1.VALID_TO, SysDate+1) > SysDate
                    order by
                        m1.NAME asc
                    ";

                DataTable dt = dbl.GetDataTable(sxQuery, param);

                AddToCache(sxCacheKey, dt, CacheType.Daily);
                
            }
            return (DataTable)HttpRuntime.Cache[sxCacheKey];
        }        

        public override String GetCacheFilename()
        {
            return KCache.Filename.FEED_SUBSCRIPTION;
        }
    }
}